<?php

namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\WxPayController;
use App\Models\AfterSales;
use App\Models\FinancialStatementsLog;
use App\Models\Order;
use App\Util\Tool;
use Illuminate\Http\Request;
use App\Service\PaymentService;
use mysql_xdevapi\Exception;

/**
 * 此项目由百凯伦公司研发，后面省略一万字
 */
class AgreeController extends Controller
{
    //同意退款
    public function store(Request $request)
    {
        $id = $request->id ?? 0;
//        $payment = new PaymentService();
        $payment = new PaymentController();
        $aftersales = new AfterSales();
        $where = [];
        $where [] = ['after_status', '=', 0];
        $where [] = ['id', '=', $id];
        $row = $aftersales->where($where)->first();
        if (empty($row)) {
            $result = [
                'code' => 0,
                "msg" => "售后订单不存在!",
                'data' => []
            ];
            return response()->json($result);
        }
        $orderId = $row->order_id ?? '';
        $orderObj = new Order();
        $order = $orderObj->where([['id', '=', $orderId]])->first();
        if (empty($order)) {
            $result = [
                'code' => 0,
                "msg" => "该订单不存在",
                'data' => []
            ];
            return response()->json($result);
        }
        $dt["user_id"] =$order->user_id;
        $dt["title"] = "同意售后消息提醒";
        $dt["content"] = "您的售后申请已申请通过，退款将原路进行返还。";
        Tool::startPush($dt);
        if ($order->pay_type == 1) {//微信
            $obj = new WxPayController();
            $lit['sn'] = $order->sn ?? '';
            $lit['return_sn'] = $orderObj->orderNumber();
            $lit['price'] = $order->order_price ?? '';
            $rest = $obj->returnBack($lit);
//            if ($rest == '退款成功') {
//                $row->after_status = 1;//已完成
//                $row->status = 1;
//                $order->order_status = 3;//售后完成
//                try {
//                    $row->save();
//                    $order->save();
//                    (new FinancialStatementsLog())->setAfterAgreeGoodsOrderBack($row->order_id);
//                } catch (Exception $exception) {
//                    return $exception->getMessage();
//                }
                $result = [
                    'code' => 1,
                    "msg" => "售后成功!",
                    'data' => []
                ];
                return response()->json($result);
//            }
        } elseif ($order->pay_type == 2) {//支付宝
            $lit['sn'] = $order->sn ?? '';
            $lit['money'] = $order->order_price ?? '';
            $lit['back_sn'] = $orderObj->orderNumber();
            $res = $payment->doReturn($lit);
            if ($res == 'success') {
                $row->after_status = 1;//已完成
                $row->status = 1;
                $order->order_status = 3;//售后完成
                try {
                    $row->save();
                    $order->save();
                    $log = (new FinancialStatementsLog())->setAfterAgreeGoodsOrderBack($row->order_id);
                    $result = [
                        'code' => 1,
                        "msg" => "售后成功!",
                        'data' => []
                    ];
                    if ($log == true) {
                        return response()->json($result);
                    } else {
                        $result = [
                            'code' => 0,
                            "msg" => "售后失败!",
                            'data' => []
                        ];
                        return response()->json($result);
                    }
                } catch (Exception $exception) {
                    return $exception->getMessage();
                }

            }
        } elseif ($order->pay_type == 3) {//余额

        } elseif ($order->pay_type == 4) {//代付

        }
        $res = ['code' => 0, "msg" => "退款失败!", 'data' => []];
        return response()->json($res);
    }
}
